package com.fuhao.orderservice.service.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class AsynService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Async
    public void bom(){
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //监控报警
        String saveOrderKey = "order-service";
        String sendValue = stringRedisTemplate.opsForValue().get(saveOrderKey);
        if(StringUtils.isEmpty(sendValue)){
            System.out.println("紧急短信：用户下单失败，请立刻查找原因");
            //发送一个http请求，调用短信服务

            //临时保存短信数据，用来判断20s期间是否有发送过短信
            stringRedisTemplate.opsForValue().set(saveOrderKey, "order-service-fail",20, TimeUnit.SECONDS);
        }else{
            System.out.println("已经发送短信，20秒内不发生短信");
        }
    }
}
